package com.seari.tunnel.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.seari.tunnel.entity.ConstructionLog;
import com.seari.tunnel.entity.ConstructionLogParam;
import com.seari.tunnel.entity.ConstructionManage;
import com.seari.tunnel.entity.ConstructionType;
import com.seari.tunnel.entity.CorridorIn;
import com.seari.tunnel.entity.CorridorOut;
import com.seari.tunnel.entity.CorridorOutParam;
import com.seari.tunnel.entity.DeductHistory;
import com.seari.tunnel.entity.DeductStandard;
import com.seari.tunnel.entity.PipelineArchives;
import com.seari.tunnel.entity.ServiceApplication;
import com.seari.tunnel.entity.ServiceApplicationParam;
import com.seari.tunnel.entity.ServiceType;
import com.seari.tunnel.entity.SupervisionLog;
import com.seari.tunnel.mapper.ConstructionLogMapper;
import com.seari.tunnel.mapper.ConstructionManageMapper;
import com.seari.tunnel.mapper.ConstructionTypeMapper;
import com.seari.tunnel.mapper.CorridorInMapper;
import com.seari.tunnel.mapper.CorridorOutMapper;
import com.seari.tunnel.mapper.DeductHistoryMapper;
import com.seari.tunnel.mapper.DeductStandardMapper;
import com.seari.tunnel.mapper.ServiceApplicationMapper;
import com.seari.tunnel.mapper.ServiceTypeMapper;
import com.seari.tunnel.mapper.SupervisionLogMapper;
import com.seari.tunnel.service.IConstructionService;

@Service(IConstructionService.SERVICE_NAME)
public class ConstructionServiceImpl implements IConstructionService{
	@Autowired
	CorridorInMapper corridorInMapper;
	@Autowired
	ConstructionTypeMapper constructionTypeMapper;
	@Autowired
	ConstructionManageMapper constructionManageMapper;
	@Autowired
	ServiceTypeMapper serviceTypeMapper;
	@Autowired
	ServiceApplicationMapper serviceApplicationMapper;
	@Autowired
	DeductStandardMapper deductStandardMapper;
	@Autowired
	ConstructionLogMapper constructionLogMapper;
	@Autowired
	DeductHistoryMapper deductHistoryMapper;
	@Autowired
	CorridorOutMapper corridorOutMapper;
	@Autowired
	SupervisionLogMapper supervisionLogMapper;
	
	@Override
	public List<CorridorIn> findOverComp() {
		
		return corridorInMapper.getAgreements();
	}

	@Override
	public List<ConstructionType> findConType() {
		
		return constructionTypeMapper.getConType();
	}

	@Override
	public void addConstructionManage(ConstructionManage constructionManage) {
		constructionManageMapper.addConstructionManage(constructionManage);
		
	}

	@Override
	public List<ConstructionManage> findConManageGrid() {
		
		return constructionManageMapper.findConManages();
	}

	@Override
	public List<ServiceType> findServiceType() {
		
		return serviceTypeMapper.getServiceType();
	}

	@Override
	public void addServiceApplication(ServiceApplication serviceApplication) {
		serviceApplicationMapper.addServiceApplication(serviceApplication);
		
	}

	@Override
	public List<ServiceApplication> findServiceApplication() {
		
		return serviceApplicationMapper.findServiceApplication();
	}

	@Override
	public List<DeductStandard> findDeductStandard() {
		
		return deductStandardMapper.getDeductStandard();
	}

	@Override
	public void addConstructionLog(ConstructionLog constructionLog) {
		constructionLogMapper.addConstructionLog(constructionLog);
		
	}

	@Transactional
	@Override
	public void addClAndDh(ConstructionLog constructionLog, int dsId) throws RuntimeException{
		int inId = constructionLog.getIn_ID();
		DeductStandard ds = deductStandardMapper.findFeeById(dsId);
		constructionLogMapper.addConstructionLog(constructionLog);
		int clId = constructionLog.getId();
		DeductHistory deductHistory = new DeductHistory();
		deductHistory.setCl_id(clId);
		deductHistory.setDs_id(dsId);
		deductHistoryMapper.addDeductHistory(deductHistory);
		corridorOutMapper.updateBalance(inId, ds.getFee());
	}

	@Override
	public List<ConstructionLog> findConstructionLog() {
		return constructionLogMapper.getConstructionLog();
	}

	@Override
	public List<CorridorOut> findUnpayComp() {
		
		return corridorOutMapper.getUnpayComp();
	}

	@Override
	public void updateBond(CorridorOut corridorOut) {
		corridorOutMapper.updateBond(corridorOut);
		
	}

	@Override
	public List<CorridorOut> findBondGrid() {
		return corridorOutMapper.findBondGrid();
	}

	@Override
	public void updateBack(CorridorOut corridorOut) {
		corridorOutMapper.updateBack(corridorOut);
		
	}

	@Override
	public List<DeductStandard> findDeductListById(int coId) {
		
		return deductStandardMapper.findDeductListById(coId);
	}

	@Override
	public List<ConstructionLogParam> getConstructionLog() {
		
		return constructionLogMapper.getCLParam();
	}

	@Transactional
	@Override
	public void deleteConstructionLog(int id)throws RuntimeException{
		ConstructionLog constructionLog = constructionLogMapper.getConstructionLogById(id);
		if("2".equals(constructionLog.getType())){
			DeductStandard deductStandard = deductStandardMapper.findFeeByCLId(id);
			
			corridorOutMapper.updateBackBalance(constructionLog.getIn_ID(), deductStandard.getFee());
		}
		constructionLogMapper.deleteConstructionLog(id);
		
	}

	@Override
	public void deleteConstructionManage(int id) {
		constructionManageMapper.deleteConstructionManage(id);
		
	}

	@Override
	public List<ServiceApplicationParam> findServiceParam() {
		
		return serviceApplicationMapper.findServiceParam();
	}

	@Override
	public void deleteServiceApplication(int id) {
		
		serviceApplicationMapper.deleteServiceApplication(id);
	}

	@Override
	public List<CorridorOutParam> findBondParam() {
		return corridorOutMapper.findBondParam();
	}

	@Override
	public void deleteBond(int id) {
		corridorOutMapper.updateBondStatus(id);
		
	}

	@Override
	public List<SupervisionLog> findSupervisionLog() {
		
		return supervisionLogMapper.findSupervisionLog();
	}

	@Override
	public List<CorridorOut> findFinishComp() {
		
		return corridorOutMapper.findFinishComp();
	}

	@Override
	public List<PipelineArchives> findPipelineArchives(String comp) {
		
		return corridorInMapper.findPipelineArchives(comp);
	}

	@Override
	public CorridorIn findCorridorInById(int id) {
		
		return corridorInMapper.findCorridorInById(id);
	}

	@Override
	public void updateOwnership(int id) {
		corridorInMapper.updateOwnership(id);
		
	}
	
	@Override
	public void updateregulator(int id) {
		corridorInMapper.updateregulator(id);
		
	}
	
	@Override
	public void updateconstructionSector(int id) {
		corridorInMapper.updateconstructionSector(id);
		
	}
	
	@Override
	public void updateprojectManager(int id) {
		corridorInMapper.updateprojectManager(id);
		
	}

	@Override
	public void updatefileField1(CorridorIn corridorIn){
		corridorInMapper.updatefileField1(corridorIn);
	}

	@Override
	public void updatefileField2(CorridorIn corridorIn){
		corridorInMapper.updatefileField2(corridorIn);
	}

	@Override
	public void updatefileField3(CorridorIn corridorIn){
		corridorInMapper.updatefileField3(corridorIn);
	}
}
